<?php
/**
  * @Author:      lyt
  * @DateTime:    2016-02-29 16:19:45
  * @Description: 门店的出库单
  */
class MaintenanceOutList extends Eloquent
{
    protected $table = "maintenance_out_list";

    

    public static $out_type = array(
        "1"=>"内部领用",
        "2"=>"退货",
        "3"=>"销售",
    );

    public static $methed = array(
        "1"=>"领用",
        "2"=>"调用",
    );

    public static $type = array(
        "1"=>"内部领用",
        "2"=>"退货出库",
        "3"=>"订单出库",
    );
   
    /**
     * @Author:      lyt
     * @DateTime:    2016-03-02 10:50:32
     * @Description: 传入一个出库单  执行该出库单的 耗材出库
     *
     */
    public static function implement($mol,$user_id){
    	return DB::transaction(function () use ($mol,$user_id) {
    	    $time=date('Y-m-d H:i:s',time());
    		$moi=MaintenanceOutItem::where("list_id",$mol->id)->where("status",0)->get();
            if(sizeof($moi)<=0){//说明该出库单是空的 
                $response = Lang::get("scs_error.maintenance_out_empty_60014");
                return $response;
            }
    		foreach ($moi as $_moi) {
                $msi=MaintenanceStorageItem::find($_moi->storage_id);
                if($msi->current_number<$_moi->number){//如果 该批次的 剩余数量不足以出库 报错
                    DB::rollback();
                    $response = Lang::get("scs_error.maintenance_out_numbererror_60006");
                    $response["result"]=array($_moi->name.'' .$_moi->model." 数量".$_moi->number."超过可出库数量");
                    return $response;
                }    			
    		    //更改 该批次的 剩余数量
                $msi->current_number=$msi->current_number-$_moi->number;
                $msi->save();
                //更改该耗材的库存数量 
                $mw=MaintenanceWarehouse::where("shop_id",$_moi->shop_id)->where("item_id",$msi->item_id)->first();
    			$mw->number=$mw->number-$_moi->number;
                $mw->save();
    			//记录 库存操作记录
    			$mwf=new MaintenanceWarehouseFlow;
    			$mwf->mw_id=$mw->id;
    			$mwf->type=2;//出库
    			$mwf->item_id=$_moi->id;
    			$mwf->number=$_moi->number;
    			$mwf->created_by=$user_id;
    			$mwf->save();
    			//更改 耗材出库单库单的状态
    			$_moi->status=1;
    			$_moi->implement_time=$time;
    			$_moi->save();
    		}
    		//更改入库单 的状态 执行的操作人
    		$mol->status=1;
    		$mol->implement_by=$user_id;
    		$mol->implement_time=$time;
    		$mol->save();
            return array('code' => 0, 'msg' => 'ok', 'result' => array("id"=>$mol->id));
        });
    }


    /**
     * @Author:      lyt
     * @DateTime:    2016-03-03 14:52:36
     * @Description: 传入一个 出库申请的 集合 和用户 返回处理结果
     */
    public static function implementOrder($out_orders,$user){
        return DB::transaction(function () use ($out_orders,$user) {
            $time=date('Y-m-d H:i:s',time());
            $msl=new MaintenanceOutList;
            $id=Common::createId();
            $msl->id=$id;
            $msl->shop_id=$user->shop_id;
            $msl->date=date("Y-m-d",time());
            $msl->type=3; //代表订单出库
            $msl->consuming_id=$user->shop_id;
            $msl->consuming_type=1;//代表是 门店
            $msl->out_type=3;//使用方式是销售
            // $msl->methed=1;//领用方式是领用 不存在领用方式
            $msl->status=1;
            $msl->cust_id=$user->id;
            $msl->desc="订单出库";
            $msl->create_by=$user->id;
            $msl->implement_by=$user->id;
            $msl->implement_time=$time;
            $msl->save();            
            foreach ($out_orders as $_od) {
                $mw=MaintenanceWarehouse::where("shop_id",$user->shop_id)->where("item_id",$_od->item_id)->lockForUpdate()->first();
                if(is_null($mw)||$mw->number<$_od->number){
                    DB::rollback();
                    $response = Lang::get("scs_error.maintenance_mw_number_60011");
                    $response["result"]=array('编号' .$_od->id."出库单 数量".$_od->number."超过可出库数量");
                    return $response;
                }
                //如果选定了 批次 优先出这个批次的
                $chuku_number=0;//选定的耗材出库批次数
                $finally_arr=$use_id=array();
                if($_od->temp_storage_id!=0){//说明选定了 批次
                    $msi=MaintenanceStorageItem::where("id",$_od->temp_storage_id)->where("shop_id",$user->shop_id)->where("status",1)
                    ->where("type",1)
                    ->where("item_type",$_od->type)
                    ->where("item_id",$_od->item_id)->first();
                    if(!$msi){  
                        DB::rollback();
                        $response = Lang::get("scs_error.maintenance_sitem_notexit_60004");
                        $response["result"]=array('编号' .$_od->id."出库单 选择批次 ".$_od->temp_storage_id." 错误");
                        return $response;
                    }
                    if($msi->current_number>0){
                        if($msi->current_number>=$_od->number){//如果当前库存大于 要出库数量
                            $msi->user_number=$_od->number;                       
                        }else{//说明满足不了 需要出库的数量
                            $msi->user_number=$msi->current_number;
                        }
                        $finally_arr[]=$msi;
                        $use_id[]=$msi->id;
                        $chuku_number=$msi->user_number;
                    }
                    
                }
                //选定的操作的耗材入库批次处理完毕
                $msi_arr=MaintenanceStorageItem::getUseStorageItem($user->shop_id,$_od->type,$_od->item_id,$_od->number-$chuku_number,$finally_arr,$use_id);//如果库存满足 出库需要 批次 一定能找打满足出库需要的 耗材批次
                foreach ($msi_arr as $_msi) {//每个批次的去 更新批次耗材 的剩余数量                    
                    $msi=MaintenanceStorageItem::find($_msi->id);
                    $msi->current_number=$msi->current_number-$_msi->user_number;
                    $msi->save();
                    //添加 该出库单的出库耗材 批次
                    $moi=new MaintenanceOutItem;
                    $moi->list_id=$id;
                    $moi->storage_list_id=$msi->list_id;
                    $moi->storage_id=$msi->id;
                    $moi->code=$msi->code;
                    $moi->name=$msi->name;            
                    $moi->model=$msi->model;
                    $moi->tax_purchase_price=$msi->tax_purchase_price;
                    $moi->no_tax_purchase_price=$msi->no_tax_purchase_price;
                    $moi->tax_purchase_subtotal=$msi->tax_purchase_price*$_msi->user_number;
                    $moi->number=$_msi->user_number;
                    $moi->subject=$msi->subject;
                    $moi->location=$msi->location;
                    $moi->production_date=$msi->production_date;
                    $moi->own_id=$msi->own_id;
                    $moi->created_by=$user->id;
                    $moi->data_id=$msi->data_id;
                    $moi->shop_id=$user->shop_id;
                    $moi->item_type=$msi->item_type;
                    $moi->item_id=$msi->item_id;
                    $moi->pack_unit=$msi->pack_unit;
                    $moi->order_out_id=$_od->id;
                    $moi->status=1;
                    $moi->implement_time=$time;
                    $moi->save();
                    //记录 库存操作记录
                    $mwf=new MaintenanceWarehouseFlow;
                    $mwf->mw_id=$mw->id;
                    $mwf->type=2;//出库
                    $mwf->item_id=$moi->id;
                    $mwf->number=$moi->number;
                    $mwf->created_by=$user->id;
                    $mwf->save();
                }
                //更改该耗材的库存数量                 
                $mw->number=$mw->number-$_od->number;
                $mw->save();

                //更改 每个出库单的状态 修改出库单记录 为等待技师领料
                MaintenanceOutOrder::where("id",$_od->id)->update(array("status"=>1));
                // $_od->status=1;
                // $_od->save();
            }
            return array('code' => 0, 'msg' => 'ok', 'result' => array("id"=>$id));
        });
    } 


   /**
    * @Author:      lyt
    * @DateTime:    2016-10-24 14:46:17
    * @Description: 用于一键出库的借口  出入一个出库申请的集合 和用户返回结果
    */
    public static function onekeyOutOrder($out_orders,$user){
        return DB::transaction(function () use ($out_orders,$user) {
            $msg_array=array();
            $time=date('Y-m-d H:i:s',time());
            $msl=new MaintenanceOutList;
            $id=Common::createId();
            $msl->id=$id;
            $msl->shop_id=$user->shop_id;
            $msl->date=date("Y-m-d",time());
            $msl->type=3; //代表订单出库
            $msl->consuming_id=$user->shop_id;
            $msl->consuming_type=1;//代表是 门店
            $msl->out_type=3;//使用方式是销售
            // $msl->methed=1;//领用方式是领用 不存在领用方式
            $msl->status=1;
            $msl->cust_id=$user->id;
            $msl->desc="订单出库";
            $msl->create_by=$user->id;
            $msl->implement_by=$user->id;
            $msl->implement_time=$time;
            $msl->save();            
            foreach ($out_orders as $_od) {
                $mw=MaintenanceWarehouse::where("shop_id",$user->shop_id)->where("item_id",$_od->item_id)->lockForUpdate()->first();
                if(is_null($mw)||$mw->number<$_od->number){
                    // if($_od->type===0){
                        $item_info=SupplyItem::getCacheItemInfo($_od->item_id);
                        $temp_name=$item_info->name; 
                        $scf_name=$item_info->scf_name;
                        $subject=$item_info->subject_name;
                        $model=$item_info->model;
                        $series_name=$item_info->series_name;
                        $brand_name=$item_info->brand_name;
                        $unit=$item_info->unit;
                        $specification=$item_info->specification;
                    // }else{
                    //     $item_info=SupplyItemNonStandard::getCacheItemInfo($_od->item_id);
                    //     $temp_name=$item_info->series_name.' '.$item_info->model; 
                    //     $scf_name=$item_info->scf_name;
                    //     $subject=$item_info->subject_name;
                    //     $model=$item_info->model;
                    //     $series_name=$item_info->series_name;
                    //     $brand_name=$item_info->brand_name;
                    //     $unit=$item_info->unit;
                    //     $specification=$item_info->specification;
                    // // }
                    $msg_array[]=array(
                        "name"=>$temp_name,
                        "subject"=>$subject,
                        "scf_name"=>$scf_name,
                        "model"=>$model,
                        "series_name"=>$series_name,
                        "brand_name"=>$brand_name,
                        "unit"=>$unit,
                        "number"=>$_od->number,
                        "date_number"=>isset($mw)?$mw->number:0,
                        "specification"=>$specification,
                        "desc"=>"库存不足",
                    );                    
                    MaintenanceOutOrder::where("id",$_od->id)->update(array("status"=>4));
                    continue;
                }
                //如果选定了 批次 优先出这个批次的
                $chuku_number=0;//选定的耗材出库批次数
                $finally_arr=$use_id=array();
                // if($_od->temp_storage_id!=0){//说明选定了 批次
                //     $msi=MaintenanceStorageItem::where("id",$_od->temp_storage_id)->where("shop_id",$user->shop_id)->where("status",1)
                //     ->where("type",1)
                //     ->where("item_type",$_od->type)
                //     ->where("item_id",$_od->item_id)->first();
                //     if(!$msi){  
                //         DB::rollback();
                //         $response = Lang::get("scs_error.maintenance_sitem_notexit_60004");
                //         $response["result"]=array('编号' .$_od->id."出库单 选择批次 ".$_od->temp_storage_id." 错误");
                //         return $response;
                //     }
                //     if($msi->current_number>0){
                //         if($msi->current_number>=$_od->number){//如果当前库存大于 要出库数量
                //             $msi->user_number=$_od->number;                       
                //         }else{//说明满足不了 需要出库的数量
                //             $msi->user_number=$msi->current_number;
                //         }
                //         $finally_arr[]=$msi;
                //         $use_id[]=$msi->id;
                //         $chuku_number=$msi->user_number;
                //     }
                    
                // }
                //选定的操作的耗材入库批次处理完毕
                $msi_arr=MaintenanceStorageItem::getUseStorageItem($user->shop_id,$_od->type,$_od->item_id,$_od->number-$chuku_number,$finally_arr,$use_id);//如果库存满足 出库需要 批次 一定能找打满足出库需要的 耗材批次
                foreach ($msi_arr as $_msi) {//每个批次的去 更新批次耗材 的剩余数量                    
                    $msi=MaintenanceStorageItem::find($_msi->id);
                    $msi->current_number=$msi->current_number-$_msi->user_number;
                    $msi->save();
                    //添加 该出库单的出库耗材 批次
                    $moi=new MaintenanceOutItem;
                    $moi->list_id=$id;
                    $moi->storage_list_id=$msi->list_id;
                    $moi->storage_id=$msi->id;
                    $moi->code=$msi->code;
                    $moi->name=$msi->name;            
                    $moi->model=$msi->model;
                    $moi->tax_purchase_price=$msi->tax_purchase_price;
                    $moi->no_tax_purchase_price=$msi->no_tax_purchase_price;
                    $moi->tax_purchase_subtotal=$msi->tax_purchase_price*$_msi->user_number;
                    $moi->number=$_msi->user_number;
                    $moi->subject=$msi->subject;
                    $moi->location=$msi->location;
                    $moi->production_date=$msi->production_date;
                    $moi->own_id=$msi->own_id;
                    $moi->created_by=$user->id;
                    $moi->data_id=$msi->data_id;
                    $moi->shop_id=$user->shop_id;
                    $moi->item_type=$msi->item_type;
                    $moi->item_id=$msi->item_id;
                    $moi->pack_unit=$msi->pack_unit;
                    $moi->order_out_id=$_od->id;
                    $moi->status=1;
                    $moi->implement_time=$time;
                    $moi->save();
                    //记录 库存操作记录
                    $mwf=new MaintenanceWarehouseFlow;
                    $mwf->mw_id=$mw->id;
                    $mwf->type=2;//出库
                    $mwf->item_id=$moi->id;
                    $mwf->number=$moi->number;
                    $mwf->created_by=$user->id;
                    $mwf->save();
                }
                //更改该耗材的库存数量                 
                $mw->number=$mw->number-$_od->number;
                $mw->save();

                //更改 每个出库单的状态 修改出库单记录 为等待技师领料
                MaintenanceOutOrder::where("id",$_od->id)->update(array("status"=>1));
                // $_od->status=1;
                // $_od->save();
            }
            return array('code' => 0, 'msg' => 'ok', 'result' => array("id"=>$id,"date"=>$msg_array));
        });
    } 
}